import com.mind.jenkins.pipeline.utils.BuildConfig

def call() {
    dockerNode.golangBuildDocker {
    
    if (currentBuild.fullProjectName == "mind-web-task"){
        properties([
                parameters([
                        choice(choices: ['EVOFLOW-PRE', 'EVOFLOW-PROD', 'EVOFLOW-DEV'], description: '选择发布环境,默认pre', name: 'ENV'),
                        choice(choices: ['pre-release', 'release'], description: '选择分支,默认pre-release', name: 'BRANCH'),
                        choice(choices: [
                                'clear-payment-gold',
                                'task-notice',
                                'matter-doc',
                                'matter-notice',
                                'user-product-info',
                                'check-user-leave-distribution',
                                'user-attendance-result',
                                'user-attendance-detail',
                                'user-attendance-checkout',
                                'user-attendance-check-free-out',
                                'user-attendance-result-free',
                                'user-attendance-work-in',
                                'user-attendance-work-out',
                                'user-attendance-leave',
                                'evaluate'
                            ], description: '选择task任务', name: 'TASKNAME'),
                ])
        ])
    }else if (currentBuild.fullProjectName == "mind-approval-task"){
         properties([
                parameters([
                        choice(choices: ['EVOFLOW-PRE', 'EVOFLOW-PROD', 'EVOFLOW-DEV'], description: '选择发布环境,默认pre', name: 'ENV'),
                        choice(choices: ['pre-release', 'release'], description: '选择分支,默认pre-release', name: 'BRANCH'),
                        choice(choices: [
                                'check-procdef-expiration',
                                'approval-inst-todo-remind',
                            ], description: '选择task任务', name: 'TASKNAME'),
                ])
        ])
    }

        BuildConfig buildConfig
        boolean isSuccess = false
        def causes = currentBuild.getBuildCauses()
        def buildUser = causes[0]?.userId ?: 'Unknown'
        echo "Build User: ${buildUser}"
        try {
            if ("${ENV}" == "EVOFLOW-PROD"){
                stage('部署生产之前二次确认') {
                    input(message: "确定发布环境 ${ENV} 的 ${BRANCH}分支？", ok: 'Continue')
                }
            }
            stage('准备拉取代码') {
                //checkout scm
                checkout([
                        $class           : 'GitSCM',
                        branches         : [[name: BRANCH]],
                        userRemoteConfigs: scm.userRemoteConfigs
                ])
            }

            

            echo "当前发布环境：${ENV}, 当前发布分支：${BRANCH}, 当前的任务名: ${TASKNAME}" 

            String appname = jobUtil.getAppNameFromJob()
            echo "当前服务名：${appname}"

            Object commitInfo = jobUtil.getCommitInfo()

            String imageAddr = jobUtil.getImageAdd(ENV)

            String imageName
            String coskey
            stage('构建镜像') {        
                parallel 'docker': {
                    echo "Build docker image..."
                    imageName = "${imageAddr}/${TASKNAME}:${commitInfo.version}_${env.BUILD_NUMBER}"
                    dockerfile = "./Dockerfile.buildtask"
                    buildTag = "${TASKNAME}"
                    jobUtil.buildDockerImage(dockerfile, "", "golang", imageName, buildTag)
                }, 'cos': {
                     echo "构建文件上传COS中..."
                }
            }

            stage('单元测试') {
                echo "单元测试中...."
            }

            stage('质量检测') {
                echo "SonarScanner质量检查扫描中...."
            }

            stage('应用部署') {
                echo "Build success, Now Callback Cd..."
                String env = params.ENV
                String appName = params.TASKNAME
                jobUtil.createBananaBuild(appName, "cron", env, buildUser, coskey, imageName, commitInfo, params.BRANCH)
                
                
            }
        } finally {
            try {
                stage('通知') {
                    echo "通知工作群..."
                    isSuccess = currentBuild.result == null || currentBuild.result == 'SUCCESS'
                    String branch = params.BRANCH
                    String env = params.ENV
                    notifyUtil.notifyApp(jobUtil.getRepoName(), branch, env, isSuccess, buildUser)
                }
                
            } catch (err) {
                echo err.getMessage()
            }
            // deleteDir()
        }
    }
}